import router from './router'
import store from './store'
import {
  HashMap
} from "@/utils/httpCount"
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({
  showSpinner: false
}) // NProgress Configuration  禁用进度环
const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)
let methods = ['push', 'go', 'replace', 'forward', 'back']
let isPush = false
let endTime = Date.now()
methods.forEach(key => {
  let method = router[key].bind(router)
  router[key] = function (...args) {
    isPush = true
    method.apply(null, args)
  }
})

router.beforeEach(function (to, from, next) {
  NProgress.start()
  HashMap.removeAll();
  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)
  // if (toIndex) {
  //   if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
  //     store.commit('updateDirection', {
  //       direction: 'forward'
  //     })
  //   } else {
  //     // 判断是否是ios左滑返回
  //     if (!isPush && (Date.now() - endTime) < 377) {
  //       store.commit('updateDirection', {
  //         direction: ''
  //       })
  //     } else {
  //       store.commit('updateDirection', {
  //         direction: 'reverse'
  //       })
  //     }
  //   }
  // } else {
    
  //   ++historyCount
  //   history.setItem('count', historyCount)
  //   to.path !== '/' && history.setItem(to.path, historyCount)
  //   store.commit('updateDirection', {
  //     direction: 'forward'
  //   })
  
  // }
  if (fromIndex == 0) {
    if (to.path === "/home") {
      next()
      
    } else {
      next({
        path: '/',
        replace: true
      })
      history.clear()
    }
  } else {
    if (/\/http/.test(to.path)) {
      let url = to.path.split('http')[1]
      window.location.href = `http${url}`
    } else {
      next()
    }
  }
})
router.afterEach((to, from, next) => {

  NProgress.done() // finish progress bar
})
